package com.dcits.mvc.base;

import java.util.Map;

import com.dcits.business.userconfig.UserSpace;
import com.dcits.constant.ConstantGlobalAttributeName;
import com.dcits.constant.ConstantReturnCode;
import com.dcits.dto.RenderJSONBean;
import com.dcits.mvc.common.model.UserConfig;
import com.jfinal.core.Controller;
import com.jfinal.core.JFinal;

public abstract class BaseController extends Controller {
	
	/**
	 * 自定义renderJSON
	 * @param code 状态码
	 * @param data 返回数据
	 * @param msg 描述<br>
	 * 描述：公共render，所有的renderJson都必须返回RenderObject，包含处理状态、返回数据、失败描述
	 */
	public void renderSuccess(Object data, String msg) {
		RenderJSONBean renderBean = new RenderJSONBean();
		renderBean.setReturnCode(ConstantReturnCode.SUCCESS);;
		renderBean.setData(data);
		renderBean.setMsg(msg == null ? "success" : msg);
		renderJson(renderBean);
	}
	
	public void renderError(int returnCode, String msg) {
		RenderJSONBean renderBean = new RenderJSONBean();
		renderBean.setReturnCode(returnCode);;
		renderBean.setData(null);
		renderBean.setMsg(msg == null ? "error" : msg);
		renderJson(renderBean);
	}
	
	public UserSpace getUserSpace(UserConfig config) {
		Map<String, UserSpace> spaces = (Map<String, UserSpace>) JFinal.me()
				.getServletContext().getAttribute(ConstantGlobalAttributeName.USER_SPACE_ATTRITURE_NAME);
		UserSpace space = spaces.get(config.getUserKey());
		if (space == null) {
			space = new UserSpace(config);
		}
		return space;
	}
	
}
